package com.taplinker.client.core;

import java.io.File;

import android.content.Context;
import android.util.Log;

import com.taplinker.client.AppConfigContext;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.rpc.command.SimpleBaseCommand;
import com.taplinker.core.rpc.http.HttpTemplate;

public class AudioManager{
	private static AudioManager instance;
	
	private AudioManager() {
	}
	
	public static AudioManager getInstance() {
		if (instance!=null) {
			return instance;
		}
		synchronized (AudioManager.class) {
			if (instance==null) {
				instance = new AudioManager();
			}
		}
		return instance;
	}
	
	public void loadAudio(final String url, LocalCallBack callBack) {
	        if (url == null) return;
	        
	        new AudioDownloadCommand(url, callBack).execute();

	}
    
    class AudioDownloadCommand extends SimpleBaseCommand{

    	private String url;
    	private LocalCallBack callBack;
    	public AudioDownloadCommand(String url, LocalCallBack callBack) {
    		this.url = url;
    		this.callBack = callBack;
    	}
    	
    	@Override
    	public Result go() {
    		Result result = new Result();
    		String requestUrl  = url;
    		try  {
    			if(url.contains("http://")) {
    				requestUrl = new HttpTemplate().download(url, this).object.toString();	
    			}
				result.object = requestUrl;
				
				
    		} catch(Exception e) {
    			Log.d("AudioDownloadCommand", "下载异常");
    		}
    		return result;
    	}
    	

    	@Override
		protected void update(long planSize, long curSize) {
    		if(callBack != null) {
    			callBack.updateProgres(planSize, curSize);
    		}
		}

		@Override
    	public void yes(Result result) {
			if(callBack != null) {
				callBack.callBack(result.object.toString());
			}
    	}
    	
    	@Override
    	public void no(Result result) {
    		super.no(result);
    		
			Log.d("AudioDownloadCommand", "下载失败");
    	}
    }
    
    public void uploadAudio(File file, Context context, LocalCallBack localCallBack) {
		new UploadCommand(file, context, localCallBack).execute();
	}
    
    class UploadCommand extends SimpleBaseCommand{

    	private File file;
    	private LocalCallBack callBack;
    	public UploadCommand(File file, Context context, LocalCallBack callBack) {
    		super(context);
    		this.file = file;
    		this.callBack = callBack;
    	}
    	
    	public UploadCommand(String path, Context context) {
    		super(context);
    		file = new File(path);
    	}
    	
    	@Override
    	public Result go() {
    		return new HttpTemplate().upload(AppConfigContext.TAPLINKER_WEB
    				+ "/services/v2/upload"
    				, file, this);
    	}

    	@Override
    	public void yes(Result result) {
    		try {
    			if(callBack != null) {
    				callBack.callBack(result.object);
    			}
    			Log.d("UploadCommand", result.message);
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    	}

    	@Override
    	protected void update(long planSize, long curSize) {
    		callBack.updateProgres(planSize, curSize);
    	}
    	
    }
    
	public interface LocalCallBack {
		public void callBack(Object obj);
		public void updateProgres(long planSize, long curSize);
	}
}
